package demo1.servlet.userBuy;

import com.alibaba.fastjson.JSON;
import demo1.pojo.users;
import demo1.service.goodsService;
import demo1.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/payOrderForm")
public class payOrderForm extends HttpServlet {//批量支付订单
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        users users = (users) userObject;

        //转为goods对象
        int[] ids = JSON.parseObject(params, int[].class);
        userService userService = new userService();
        try {
            int num = userService.payOrderForm(ids,users);
            if (num == 1) {
                //支付成功
                resp.getWriter().write("success");
            }
            else if(num == 2){
                //钱不够
                resp.getWriter().write("error2");
            }
            else if(num == 3){
                //订单过期
                resp.getWriter().write("error3");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
